package com.atguigu.gulimall.order.config;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id="9021000122673924";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQClUwMnYo5CmAER1mD1JyaeAa6h+cWUQmh8F/hrC6MMFs/WeGzXj1zHtD+4g5hImjN2dql8z/GN9Mjtcy99dgwJ5zmsoCihD0lisMb8bnHu5u6zgEgipJKw8wjOHQ3hvDbXd6Ofpbyk72NUpGXbMVVqDcZ+RBo25QaPBmPoL0qEEfE2TRRPgfReSoQheMmzx9p+lJHvNA9uOnd/0bMjEgh5AQkPaDptWjhQLGtGAplMZFhapSt0rKAWrfv2PR7PtGmKywYdbgy0sKdjn27cppsPBmlOAC6gQS/RhnZ87C8lbftxs4TnCV82Eb4c6Tl80SHiDJEt/aD45s1FXwXR/uhFAgMBAAECggEAXjOp5WQ8CWgRm/dasfYIBBqTRkpLMcqRPLxv2Zb85eONs/hUlJNk24oykNGzEROEZWkB9FOMoUWsw9hRZBzCiVYOv/VWkUdjmm9+QtIv61/8/Z0H/W1nBJrHFqLpiQm0+ht6yXonvpeFMvKl8zruH4sP4lytzz+69/49KUzhGdUuHUm1LWE2Z55hzAom4k13MZYB3wrQKv0diFc+tc3jukvJgELD7m+YNbUzfKOtpSObgUJJbQX8OA3s4iWtlsPdjxot81OJVA6VyGkQDHZc3jjaRgCmVm9Yif00rywGV52bAQ15yibwlWV4C7FcZ3eJ0RU7qV/FpG5TjkMSqhihgQKBgQDwc7RSYOFq91Qo8pfNQWEtFFm+IXdkRoVhuh94OFiGH9cFCu+YEpazJnImDHR7ID/CFjh7jDfmDCNbicO7rT/PIrXrJPkE7CvUnKL5+Fji7ah7fkypsSl0HkTNyi7F8eIZNo0Lkv1fOfmSUqsEImMhF6jsjIfd8yQrMAKJ96a2oQKBgQCwA7CMoYytnsl8Jc8+tS8Fqevwg3tXozRyhbIuiujwFLWx+Ns9kMfLIjfaQTfjm7gabQ0HjUGVgjb1EsSQpPN+aNMZ/JuwsEkkQNyohBewmWD/K4zMPi9s78VDgqiIZxCbMrVCNPPpeCtbL9m4xZYa5D2QMpJLMjXwxa1Q4d6jJQKBgGAhUfExTE2wbdQlEK19uvUD0tEhnhLX4tFsUNKGbFXK+vLIKXu5ysf60d4GUrc68GKO8avrJhnxtl0NWdR5ci5EqUhGMsxWAzvwF3QqZpbq/tXIa6vX6UPEKiu/7i0zA/qihYcgD+33RqDKLhbsDIeZZu6TTGpen5IokotIGD2hAoGAMBUh8pjjJD6/TdHKVpgKO2La2D4iwKMN3ccWEM7TqVoO9IpF4gCeA6jaVx3/1YaheV6W85wr8Vq8BqDU36qf9cVO/cdbBJ5zrJqnbMWA7zZHIA3WjajjLcAmRDX6MiDtkWf9yhNLGllAhs2AjZA0XC5DfxPBkRcrUxW9Ld42Ap0CgYAkSeq+4RsHBqussIuXtnSkXRv+7nC0S4lD5YyzDNQawI+Djjind4mBHvMZogvV1+GmCHJcxTptrF/VIi2KtywIb+BeaRgrvkWuMwlJ2IjRkMgq26EtquPQR8msBG3qPbXGZAk48W/jhL5eeev6/3oPmeOi4LocJMnWvVcRAYJ4GA==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgpNJHEtoUeBA02ERZ3dugxpjhTjbX+W3Oes55pdx2884k/9OZi5e2lm5JOl74fumadoX0U1SGsg5XaTj1pL7Lbmm6TwO5wI4x9P8Oq5UbCq0LRP3HnbLoqfhZr1qK/IYyxKHRpzRVEgYLfO2IS+t6uQ9RdUwDLXajmlMu0iHk2YyDiT1Rp0nRz/N8TyFBPeUZiSuiYaGAKbE22Sxcyel6NULiVmA1DzLUve88LvhGYZQh8JDZZGBebpCalNYN3eHZoJSK0qeS7X4RIkqXg2PKjAEhnDMwA3XDVxkC/bbp/cz9ivPxWou4noXvYosHb867peWpfHeOczJcBVxuqpa7QIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url="http://member.xiaochao.love/memberOrder.html";

    // 签名方式
    private  String sign_type="RSA2";

    // 字符编码格式
    private  String charset="utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl="https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
